package com.project.myxima.adapters;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.project.myxima.R;
import com.project.myxima.utils.LogUtils;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

public class AlbumListAdapter extends RecyclerView.Adapter<AlbumListAdapter.InnerHolder> {

    private static final String TAG = "RecommendListAdapter";
    private onRecommendItemClickListener mItemClickListener = null;
    private List<Album> mData = new ArrayList<>();
    private onAlbumItemLongClickListener mItemLongClickListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载view
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend,parent,false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        //设置数据
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener != null) {
                    int clickPosition = (int) view.getTag();
                    mItemClickListener.onItemClick(clickPosition,mData.get(clickPosition));
                }
                LogUtils.d(TAG,"holder.itemView. click-->" + view.getTag());
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemLongClickListener != null) {
                    mItemLongClickListener.onItemLongClick(mData.get(position));
                }
                return true;
            }
        });
        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        if(mData != null){
            return mData.size();
        }
        return 0;
    }

    public void setData(List<Album> albumList) {
        if(mData != null){
            mData.clear();
            mData.addAll(albumList);
        }

        //更新UI
        notifyDataSetChanged();
    }

    public static class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(Album album) {
            //找到各个控件，设置数据
            //专辑封面
            ImageView albumCoverIv = itemView.findViewById(R.id.album_cover);
            //title
            TextView albumTitleTv = itemView.findViewById(R.id.album_title_tv);
            //描述
            TextView albumDesTv = itemView.findViewById(R.id.album_description_tv);
            //播放数量
            TextView albumPlayCountTv = itemView.findViewById(R.id.album_play_count);
            //专辑内容数量
            TextView albumContentCountTv = itemView.findViewById(R.id.album_content_size);

            //设置内容给这些控件
            String coverUrlLarge = album.getCoverUrlLarge();
            if(!TextUtils.isEmpty(coverUrlLarge)){
                Picasso.get().load(coverUrlLarge).into(albumCoverIv);
            }else{
                albumCoverIv.setImageResource(R.mipmap.ximalay_logo);
            }
            albumTitleTv.setText(album.getAlbumTitle());
            albumDesTv.setText(album.getAlbumIntro());
            albumPlayCountTv.setText(album.getPlayCount() + "");
            albumContentCountTv.setText(album.getIncludeTrackCount() +"");
        }
    }


    public void  setOnRecommendItemClickListener(onRecommendItemClickListener listener){
        this.mItemClickListener = listener;
    }

    public interface onRecommendItemClickListener{
        void onItemClick(int position, Album album);
    }


    /**
     * long click interface
     */

    public void setOnAlbumItemLongClickListener(onAlbumItemLongClickListener longClickListener){
        this.mItemLongClickListener = longClickListener;
    }

    public interface onAlbumItemLongClickListener{
        void onItemLongClick(Album album);
    }





}
